<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<jsp:include page="/admin/common/css/style_sub.jsp"></jsp:include>
<script type="text/javascript" src="<%=basePath%>js/lib/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/public.js"></script>
<script language="javascript">
$(function(){

});

//全选
function selAll(yn){
	var trId = 'roles' + yn;
	$('#' + trId + ' input').each(function(i){
		this.checked = true;
	});
}

//反选
function reverse(yn){
	var trId = 'roles' + yn;
	$('#' + trId + ' input').each(function(i){
		if(this.checked == true || this.checked == 'checked'){
			this.checked = false;
		}else{
			this.checked = true;
		}
	});
}

//添加选中的角色
function addRole(){
	$('#rolesNo input').each(function(i){
		if(this.checked == true || this.checked == 'checked'){
			var temp = $(this).parent().clone();
			$(this).parent().remove();
			$('#rolesYes').append(temp);
		}
	});
}

//移除所选的角色
function delRole(){
	$('#rolesYes input').each(function(i){
		if(this.checked == true || this.checked == 'checked'){
			var temp = $(this).parent().clone();
			$(this).parent().remove();
			$('#rolesNo').append(temp);
		}
	});
}

//保存
function save(){
	var roleIds = [];
	var subIds = [];
	var roles = $('#rolesYes span');
	for(var i=0; i<roles.length; i++){
		roleIds.push($(roles[i]).attr('roleid'));
		subIds.push($(roles[i]).attr('subid'));
	}
	
	var params = {
		ct: (new Date()).getTime(),
		'user.id': '${user.id}',
		'user.roleIds': roleIds.join(',') + '_' + subIds.join(',')
	};
	$.getJSON("<%=basePath%>basic/sys/user_saveUserRole.action", params, function(json){
		if(json.resultCode == 'success'){
			if(json.resultCode == 'success'){
				alert('保存用户角色信息成功！');
				window.location.href = window.location.href;
			}else{
				alert('保存用户角色信息失败，请联系管理员！');
			}
		}
	});
}

</script>
</head>
<body>
	<div class="title">用户角色维护</div>
	<div class="editorTab">
		<table>
			<tbody id="tbody">
				<tr>
					<th>姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名</th>
					<td>
						<input type="text" name="user.rname" id="rname" value="${user.rname}"  readonly="readonly" class="unenterTextbox" />
					</td>
					<th>所在部门</th>
					<td>
						<input type="text" name="user.org_name" id="org_name" value="${user.org_name}" readonly="readonly" class="unenterTextbox"/>
						<input type="hidden" name="user.org_id" value="${user.org_id}"/>
					</td>
				</tr>
				<tr>
					<th rowspan="2">可选角色</th>
					<td id="rolesNo">
						<s:iterator value="%{roleList_no}">
							<span roleid="${id}" subid="${sub_id}" rolename="${rname}" subname="${subSysName}">
								(${subSysName})&nbsp;&nbsp;${rname}
								<input type="checkbox"/><br/>
							</span>
						</s:iterator>
					</td>
					<th rowspan="2">已选角色</th>
					<td id="rolesYes">
						<s:iterator value="%{roleList_yes}">
							<span roleid="${id}" subid="${sub_id}" rolename="${rname}" subname="${subSysName}">
								(${subSysName})&nbsp;&nbsp;${rname}
								<input type="checkbox"/><br/>
							</span>
						</s:iterator>
					</td>
				</tr>
				<tr id="addRow">
					<td>
						<span class="btn">
							<input type="button" value="全选" onclick="selAll('No');"/>
						</span>
						<span class="btn">
							<input type="button" value="反选" onclick="reverse('No');"/>
						</span>
						<span class="btn">
							<input type="button" value="-->" onclick="addRole();"/>
						</span>
					</td>
					<td>
						<span class="btn">
							<input type="button" value="<--" onclick="delRole();"/>
						</span>
						<span class="btn">
							<input type="button" value="反选" onclick="reverse('Yes');"/>
						</span>
						<span class="btn">
							<input type="button" value="全选" onclick="selAll('Yes');"/>
						</span>
					</td>
				</tr>
			</tbody>
		</table>
	</div>
	<div class="btns">
		<span class="btn"><input type="button" value="保存" onclick="save();"/></span>
		<span class="btn">
			<input type="button" id="cancelBtn" value="返回" 
				onclick="javascript:window.location.href='<%=basePath%>basic/sys/user_selectUser.action'"/>
		</span>
	</div>
</body>
</html>
